package com.da.javatest.leetCode;

import java.math.BigInteger;

/**
 * @author chenlida
 * @date 2019/1/17 20:21
 * @description
 */
public class Code67 {
    public static void main(String[] args) {
        System.out.println(new Code67().addBinary("1010", "1011"));
    }

    public String addBinary(String a, String b) {
        //return new BigInteger(a, 2).add(new BigInteger(b, 2)).toString(2);
        StringBuilder sb = new StringBuilder();
        char[] c1 = a.toCharArray();
        char[] c2 = b.toCharArray();
        int l1 = c1.length - 1;
        int l2 = c2.length - 1;
        boolean flag = false;
        int max = Math.max(c1.length, c2.length);
        for (int i = 0; i < max; i++) {
            int n1 = l1 >= 0 ? Integer.parseInt(c1[l1--] + "") : 0;
            int n2 = l2 >= 0 ? Integer.parseInt(c2[l2--] + "") : 0;
            int n = n1 + n2;
            if (flag) {
                n++;
            }
            if (n == 0) {
                flag = false;
                sb.insert(0, 0);
            }
            if (n == 1) {
                flag = false;
                sb.insert(0, 1);
            }
            if (n == 2) {
                flag = true;
                sb.insert(0, 0);
            }
            if (n == 3) {
                flag = true;
                sb.insert(0, 1);
            }
        }
        if (flag) {
            sb.insert(0, 1);
        }
        return sb.toString();
    }
}
